﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Net;
using xClient.Framework;
using xClient.Framework.StaticResource;

namespace cMES4001.Services
{
    public class ServiceMethod
    {
        public static RestClient _restClient = new RestClient(StaticDictionary.Dictionary.ServiceUrl);

        public static ExecutionResult<dynamic> GetGroupNameList(string vSection = null)
        {
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/group/getnamelist";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                if (!string.IsNullOrEmpty(vSection))
                {
                    _request.AddQueryParameter("section", vSection);
                }
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取工站信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> GetLineNameList()
        {
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/line/namelist";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;
                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);
                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取线别信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> GetStationRecInfo(dynamic vQCondition)
        {
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            string api_path = "/reports/stationrec/";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddQueryParameter("work_date", vQCondition.work_date);
                _request.AddQueryParameter("mo", vQCondition.mo);
                _request.AddQueryParameter("model", vQCondition.model);                
                _request.AddQueryParameter("group", vQCondition.group);
                _request.AddQueryParameter("line", vQCondition.line);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;
                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);
                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取产能信息异常." + ex.Message;
            }

            return result;
        }
    }
}
